package net.keebook.webapp.model;

import java.util.Locale;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.openid4java.discovery.DiscoveryInformation;
import org.scribe.model.Token;

public class SessionModel {
	private Long userId = null;
	private Long keeBookId = null;
	private Boolean admin = false;
	private Boolean authKeyChecked = false;
	private String invitationId = "";
	private Token oAuthRequestToken = null;
	private Boolean migrateAccount = false;
	private Boolean requireProfileCompletion = false;
	private DiscoveryInformation discoveryInformation = null;
	private Locale locale = new Locale("en", "US");
	
	private SessionModel() {
		clear();
	}
	
	public void clear() {
		setUserId(null);
		setKeeBookId(null);
		setAdmin(false);
		setMigrateAccount(false);
	}
	
	public static SessionModel getInstance(FacesContext context) {
		return getInstance((HttpSession)context.getExternalContext().getSession(true));
	}
	
	public static SessionModel getInstance(HttpSession session) {
		SessionModel model = (SessionModel)session.getAttribute("sessionModel");
		if (model == null) {
			model = new SessionModel();
			session.setAttribute("sessionModel", model);
		}
		return model;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Long getKeeBookId() {
		return keeBookId;
	}

	public void setKeeBookId(Long keeBookId) {
		this.keeBookId = keeBookId;
	}
	
	public Boolean getLoggedIn() {
		return (userId != null && userId > 0);
	}
	
	public Boolean getKeeBookSelected() {
		return (keeBookId != null && keeBookId > 0);
	}

	public Boolean getAdmin() {
		return admin;
	}

	public void setAdmin(Boolean admin) {
		this.admin = admin;
	}

	@Override
	public String toString() {
		return "SessionModel [userId=" + userId + ", keeBookId=" + keeBookId
				+ ", admin=" + admin + "]";
	}

	public String getInvitationId() {
		return invitationId;
	}

	public void setInvitationId(String invitationId) {
		this.invitationId = invitationId;
	}

	public Boolean getAuthKeyChecked() {
		return authKeyChecked;
	}

	public void setAuthKeyChecked(Boolean authKeyChecked) {
		this.authKeyChecked = authKeyChecked;
	}

	public DiscoveryInformation getDiscoveryInformation() {
		return discoveryInformation;
	}

	public void setDiscoveryInformation(DiscoveryInformation discoveryInformation) {
		this.discoveryInformation = discoveryInformation;
	}

	public Token getoAuthRequestToken() {
		return oAuthRequestToken;
	}

	public void setoAuthRequestToken(Token oAuthRequestToken) {
		this.oAuthRequestToken = oAuthRequestToken;
	}

	public Boolean getMigrateAccount() {
		return migrateAccount;
	}

	public void setMigrateAccount(Boolean migrateAccount) {
		this.migrateAccount = migrateAccount;
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public Boolean getRequireProfileCompletion() {
		return requireProfileCompletion;
	}

	public void setRequireProfileCompletion(Boolean requireProfileCompletion) {
		this.requireProfileCompletion = requireProfileCompletion;
	}
}
